package com.bbzn.device.client.service.emq.alarm;

import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.constant.TitleConstant;
import com.bbzn.device.client.dataobject.Switch;
import com.bbzn.device.client.dto.ct.data.EmqAlarmDTO;
import com.bbzn.device.client.service.EmqStrategy;
import com.bbzn.device.client.service.SwitchService;
import com.bbzn.device.client.service.emq.alarm.EmqBaseAlarm;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author wulongbo
 * @Date 2021/12/3 15:42
 * @Version 1.0
 */
@Component
public class EmqFenceAlarmStrategy extends EmqBaseAlarm implements EmqStrategy {

    @Resource
    private SwitchService switchService;

    @Override
    public String emqAction(String payLoad) {
        EmqAlarmDTO emqAlarm = new EmqAlarmDTO();
        List<String> list = new ArrayList<>();
        list.add("imei");
        list.add("fenceName");
        list.add("fenceCode");
        list.add("fenceType");
        list.add("type");
        list.add("lat");
        list.add("lon");
        list.add("location");

        /*Long userId = this.checkPayload(list, payLoad);
        if (userId == null) {
            return "";
        }*/

        JSONObject jsonObject = JSONObject.parseObject(payLoad);
        // "1":恢复正常  "2":报警(触发围栏) "3":停留在围栏内
        Integer fenceCode = jsonObject.getInteger("fenceCode");
        // 0:重点 , 1:电子
        Integer fenceType = jsonObject.getInteger("fenceType");

        String cnInfo = null;
        String enInfo = null;
        int alarmType = 0;
        if (fenceCode == 1) {
            if (fenceType == 0) { // 重点围栏
                cnInfo = "正常消息：已经离开重点围栏";
                enInfo = "normal message：has left the key fence";
                alarmType = 4;
            }
            if (fenceType == 1) { // 电子围栏
                cnInfo = "正常消息：已经进入电子围栏";
                enInfo = "normal message：has entered the electronic fence";
                alarmType = 3;
            }
        } else if (fenceCode == 2) {
            if (fenceType == 0) { // 重点围栏
                cnInfo = "异常消息：已经进入重点围栏";
                enInfo = "normal message：has entered the key fence";
                alarmType = 24;
            }
            if (fenceType == 1) { // 电子围栏
                cnInfo = "异常消息：已经离开电子围栏";
                enInfo = "normal message：has left the electronic fence";
                alarmType = 23;
            }
        } else {
            return "";
        }
        // 定位类型【1：GPS / 2：传感器 / 4：缓存定位 / 5：wifi定位 / 6：基站定位】
        Integer type = jsonObject.getInteger("type");
        if (null == type || "".equals(type)){
            type = 5;
        }
        String lat = jsonObject.getString("lat");
        String lon = jsonObject.getString("lon");
        String location = jsonObject.getString("location");

        emqAlarm.setImei(jsonObject.getString("imei"));
        emqAlarm.setAlarmType(alarmType);
        emqAlarm.setTitle(fenceCode == 1 ? TitleConstant.FENCE_NORMAL_ALARM : TitleConstant.FENCE_ABNORMAL_ALARM);
        emqAlarm.setEnTitle(fenceCode == 1 ? TitleConstant.FENCE_NORMAL_ALARM_EN : TitleConstant.FENCE_ABNORMAL_ALARM_EN);
        String cnSign = MessageFormat.format("[围栏报警]-{0}", cnInfo);
        String enSign = MessageFormat.format("[fence alarm] -{0}", enInfo);
        emqAlarm.setCnSign(cnSign);
        emqAlarm.setEnSign(enSign);
        emqAlarm.setLat(lat);
        emqAlarm.setLon(lon);
        emqAlarm.setLocation(location);
        emqAlarm.setType(type);

        // web端围栏告警/短信/邮箱开关检查
        Switch need = new Switch();
        need.setImei(jsonObject.getString("imei"));
        List<Boolean> checkResult = switchService.checkSwitchByFence(need, fenceCode);
        emqAlarm.setBooleanList(checkResult);
        this.alarmRun(emqAlarm);
        return "";
    }
}

